package com.d3works.apps.businessproject.service;

import com.d3works.apps.account.db.AnalyticMoveLine;
import com.d3works.apps.account.service.analytic.AnalyticMoveLineService;
import com.d3works.apps.account.service.app.AppAccountService;
import com.d3works.apps.account.service.config.AccountConfigService;
import com.d3works.apps.base.service.CurrencyService;
import com.d3works.apps.base.service.PriceListService;
import com.d3works.apps.base.service.ProductMultipleQtyService;
import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.apps.base.service.pricing.PricingService;
import com.d3works.apps.base.service.tax.AccountManagementService;
import com.d3works.apps.base.service.tax.TaxService;
import com.d3works.apps.project.db.Project;
import com.d3works.apps.sale.db.SaleOrderLine;
import com.d3works.apps.sale.db.repo.SaleOrderLineRepository;
import com.d3works.apps.sale.service.app.AppSaleService;
import com.d3works.apps.sale.service.saleorder.SaleOrderService;
import com.d3works.apps.supplychain.service.SaleOrderLineServiceSupplyChainImpl;
import com.d3works.apps.supplychain.service.app.AppSupplychainService;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.util.List;

public class SaleOrderLineProjectServiceImpl extends SaleOrderLineServiceSupplyChainImpl
    implements SaleOrderLineProjectService {

  @Inject
  public SaleOrderLineProjectServiceImpl(
      CurrencyService currencyService,
      PriceListService priceListService,
      ProductMultipleQtyService productMultipleQtyService,
      AppBaseService appBaseService,
      AppSaleService appSaleService,
      AccountManagementService accountManagementService,
      SaleOrderLineRepository saleOrderLineRepo,
      AppAccountService appAccountService,
      AnalyticMoveLineService analyticMoveLineService,
      AppSupplychainService appSupplychainService,
      AccountConfigService accountConfigService,
      SaleOrderService saleOrderService,
      PricingService pricingService,
      TaxService taxService) {
    super(
        currencyService,
        priceListService,
        productMultipleQtyService,
        appBaseService,
        appSaleService,
        accountManagementService,
        saleOrderLineRepo,
        saleOrderService,
        appAccountService,
        analyticMoveLineService,
        appSupplychainService,
        accountConfigService,
        pricingService,
        taxService);
  }

  @Transactional
  @Override
  public void setProject(List<Long> saleOrderLineIds, Project project) {

    if (saleOrderLineIds != null) {

      List<SaleOrderLine> saleOrderLineList =
          saleOrderLineRepo.all().filter("self.id in ?1", saleOrderLineIds).fetch();

      for (SaleOrderLine line : saleOrderLineList) {
        line.setProject(project);
        saleOrderLineRepo.save(line);
      }
    }
  }

  @Override
  public SaleOrderLine createAnalyticDistributionWithTemplate(SaleOrderLine saleOrderLine) {

    SaleOrderLine soLine = super.createAnalyticDistributionWithTemplate(saleOrderLine);
    List<AnalyticMoveLine> analyticMoveLineList = soLine.getAnalyticMoveLineList();

    if (soLine.getProject() != null && analyticMoveLineList != null) {
      analyticMoveLineList.forEach(analyticLine -> analyticLine.setProject(soLine.getProject()));
    }
    return soLine;
  }

  @Override
  public SaleOrderLine updateAnalyticDistributionWithProject(SaleOrderLine saleOrderLine) {
    for (AnalyticMoveLine analyticMoveLine : saleOrderLine.getAnalyticMoveLineList()) {
      analyticMoveLine.setProject(saleOrderLine.getProject());
    }
    return saleOrderLine;
  }
}
